package zhoug.chart.formatters;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import zhoug.chart.utils.MPChartUtils;

/**
 * @Author: zhoug
 * @Date: 2024-02-07
 * @Description:
 */
public class MPPieDataSetValueFormatter extends ValueFormatter {
    private int maxTextLength = 0;//最多显示几个文字
    private boolean showValue;//是否显示数量
    private boolean showPercent;//是否显示百分比
    private int valueDecimal = 2;//小数位数
    private boolean valueFillEnd = false;//小数位不足是否需要补0
    private int percentDecimal = 2;//百分比小数位数
    private boolean percentFillEnd = false;//百分比小数位不足是否需要补0
    private PieChart pieChart;

    public MPPieDataSetValueFormatter(PieChart pieChart) {
        this.pieChart = pieChart;
    }

    private float getYValueSum() {
        if (pieChart != null) {
            PieData data = pieChart.getData();
            if (data != null) {
                return data.getYValueSum();
            }
        }
        return 0;
    }

    @Override
    public String getPieLabel(float value, PieEntry pieEntry) {
        if (pieEntry != null) {
            String label = pieEntry.getLabel();
            if (label == null) {
                label = "";
            }
            if (maxTextLength > 0 && label.length() > maxTextLength) {
                label = label.substring(0, maxTextLength) + "···";
                label= MPChartUtils.ellipsizeEnd(label,maxTextLength);
            }
            StringBuilder stringBuilder = new StringBuilder();
            if (showValue) {
                stringBuilder.append(MPChartUtils.formatDecimalUp(value, valueDecimal, valueFillEnd));
            }
            if (showPercent) {
                float yValueSum = getYValueSum();
                float percent = 0;
                if (yValueSum > 0) {
                    percent = value * 100f / yValueSum;
                }
                if (showValue) {
                    stringBuilder.append(MPChartUtils.formatString("(%s%%)", MPChartUtils.formatDecimalUp(percent, percentDecimal, percentFillEnd)));
                } else {
                    stringBuilder.append(MPChartUtils.formatString("%s%%", MPChartUtils.formatDecimalUp(percent, percentDecimal, percentFillEnd)));
                }
            }
            String s = stringBuilder.toString();
            if(s.length()>0){
                return MPChartUtils.formatString("%s: %s",label,s);
            }
            return label;
        } else {
            return "";
        }
    }


    public void setMaxTextLength(int maxTextLength) {
        this.maxTextLength = maxTextLength;
    }

    public void setShowValue(boolean showValue) {
        this.showValue = showValue;
    }

    public void setShowPercent(boolean showPercent) {
        this.showPercent = showPercent;
    }

    public void setValueDecimal(int valueDecimal) {
        this.valueDecimal = valueDecimal;
    }

    public void setValueFillEnd(boolean valueFillEnd) {
        this.valueFillEnd = valueFillEnd;
    }

    public void setPercentDecimal(int percentDecimal) {
        this.percentDecimal = percentDecimal;
    }

    public void setPercentFillEnd(boolean percentFillEnd) {
        this.percentFillEnd = percentFillEnd;
    }
}
